<?php
class Stok extends CI_Controller{
         private $limit = 10;
	function  __construct() {
        parent::__construct();
        $this->load->model('Stok_model');
	$this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->library(array('form_validation','pagination'));

      }

	function index($offset=''){

		$this->load->library('pagination'); //load library pagination
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);

		$config['base_url'] = site_url('stok/index/');
		$config['total_rows'] = $this->Stok_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		$data['jumlah_data']=$this->Stok_model->countData();

		$data['stok']	= $this->Stok_model->get_list_data($this->limit,$offset);
	//$data['bank'] = $this->Bank_model->getBank();

       $this->load->view('admin_views/stok/stok', $data);
    }

 function lookupBarang(){
	$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Stok_model->selectBarang($keyword);
        if( ! empty($query) )
        {
            $data['response'] = 'true'; 
            $data['message'] = array(); 
            foreach( $query as $row )
            {
                $data['message'][] = array( 
                                        'id'=>$row->intid_barang,
                                        'value' => $row->strnama,
                                        ''
                                     );  
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data); 
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); 
        }
	}
     

    function detail($id){
		$profile = $this->Stok_model->selectDetail($id);
        	foreach($profile as $m)
			{
				$data['strnama']			= $m->strnama;
				$data['tanggal']			= $m->tanggal;
				$data['strnama_jsatuan']	= $m->strnama_jsatuan;
				$data['strnama_cabang']		= $m->strnama_cabang;
				$data['intqty_begin']  		= $m->intqty_begin;
				$data['intqty_in']       	= $m->intqty_in;
				$data['intqty_out'] 	 	= $m->intqty_out;
                $data['intqty_end']		 	= $m->intqty_end;
				
            }
                         
		$this->load->view('admin_views/stok/detail', $data);
	}
    function add()
	{
		$this->form_validation->set_rules('intid_barang', 'Nama Barang', 'trim|required|xss_clean');
		$this->form_validation->set_rules('supplier', 'Nama Supplier', 'trim|required|xss_clean');
		$this->form_validation->set_rules('tanggal', 'Tanggal ', 'trim|required|xss_clean');
		$this->form_validation->set_rules('intqty', 'Jumlah Stock', 'trim|required|xss_clean');
		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');

		if ($this->form_validation->run() == FALSE)
		 {
                       $this->load->view('admin_views/stok/add');
		}else{

                        $this->Stok_model->insert($_POST);
			redirect('stok/stok');
                }
	}
  function edit($intid_stok)
		{
			if($_POST==NULL)
			{

				$datastok = $this->Stok_model->select($intid_stok);
				foreach ($datastok as $g)
				{
					$data['intid_stok']	 	= $g->intid_stok;
					$data['supplier']	 	= $g->supplier;
					$data['tanggal']	= $g->tanggal;
					$data['intid_barang']   	= $g->intid_barang;
					$data['intqty'] 		= $g->intqty;
				}
				$this->load->view('admin_views/stok/edit', $data);
			}else {
				$this->Stok_model->update($intid_stok);
				redirect('stok/index');
			}

                }

    function delete($intid_stok)
	{
		$this->Stok_model->delete($intid_stok);
		redirect('stok/stok');
	}


}
?>
